{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sorting Iterables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There's nothing really new here - we have seen the `sorted()` function before when we looked at sorting sequences.\n",
    "\n",
    "The `sorted()` function will in fact work with any iterable, not just sequences.\n",
    "\n",
    "Let's try this by creating a custom iterable and then sorting it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For this example, we'll create an iterable of random numbers, and then sort it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "random.seed(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "4\n",
      "9\n",
      "3\n",
      "5\n",
      "3\n",
      "2\n",
      "10\n",
      "5\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    print(random.randint(1, 10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "class RandomInts:\n",
    "    def __init__(self, length, *, seed=0, lower=0, upper=10):\n",
    "        self.length = length\n",
    "        self.seed = seed\n",
    "        self.lower = lower\n",
    "        self.upper = upper\n",
    "        \n",
    "    def __len__(self):\n",
    "        return self.length\n",
    "    \n",
    "    def __iter__(self):\n",
    "        return self.RandomIterator(self.length, \n",
    "                                   seed = self.seed, \n",
    "                                   lower = self.lower,\n",
    "                                   upper=self.upper)\n",
    "    \n",
    "    \n",
    "    class RandomIterator:\n",
    "        def __init__(self, length, *, seed, lower, upper):\n",
    "            self.length = length\n",
    "            self.lower = lower\n",
    "            self.upper = upper\n",
    "            self.num_requests = 0\n",
    "            random.seed(seed)\n",
    "            \n",
    "        def __iter__(self):\n",
    "            return self\n",
    "        \n",
    "        def __next__(self):\n",
    "            if self.num_requests >= self.length:\n",
    "                raise StopIteration\n",
    "            else:\n",
    "                result = random.randint(self.lower, self.upper)\n",
    "                self.num_requests += 1\n",
    "                return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "randoms = RandomInts(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "6\n",
      "0\n",
      "4\n",
      "8\n",
      "7\n",
      "6\n",
      "4\n",
      "7\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for num in randoms:\n",
    "    print(num)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now sort our iterable using the `sorted()` method:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 4, 4, 5, 6, 6, 6, 7, 7, 8]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(randoms)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[8, 7, 7, 6, 6, 6, 5, 4, 4, 0]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(randoms, reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
